import { OrderAction } from '../actions'
import { OrderType } from '../Types'
import { 
  GetOrderList_PENDING, 
  GetOrderList_FULFILLED, 
  GetOrderList_PAGE,
  RECORD_Order_ID,
  GetOrderDetailList_PENDING, 
  GetOrderDetailList_FULFILLED, 
  GetOrderDetailList_PAGE, } from '../Constants'
// 初始化
const initState= {
  loading: false,
  record: '',
  head: {},
  data: {
    list: [],
    pagination: {}
  },
  detailData: {
    list: [],
    pagination: {}
  }
}
export function OrderReducer(state: OrderType = initState, action: OrderAction): OrderType {
  switch (action.type) {
    case GetOrderList_PENDING || GetOrderDetailList_PENDING:
      return { ...state, loading: true }
    case GetOrderList_FULFILLED:
      return { 
        ...state, 
        loading: false, 
        data: { 
          list: action.payload.data, 
          pagination: {
            ...state.data.pagination,
            total: Number(action.payload.Out.pagecount) 
          } 
        } 
      }
    case GetOrderList_PAGE:
      return {
        ...state,
        data: { 
          pagination:{
            ...state.data.pagination,
            current: action.payload.current || 1,
            pageSize: action.payload.pageSize || 10
          }
        } 
      }
    case RECORD_Order_ID:
      return {
        ...state,
        record: action.payload
      }
    case GetOrderDetailList_FULFILLED:
      const { payload: { detial, head } } = action
      return { 
        ...state, 
        loading: false,
        head: head || {}, 
        detailData: { 
          list: detial
        } 
      }
    case GetOrderDetailList_PAGE:
      return {
        ...state,
        detailData: {
          pagination:{
            ...state.detailData.pagination,
            current: action.payload.current || 1,
            pageSize: action.payload.pageSize || 10
          }
        } 
      }
  }
  return state
}